package com.ecoolex.chinadutyfree.retailer.api.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoods;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsCategory;
import com.ecoolex.chinadutyfree.retailer.pojo.GoodsCategoryTopChildDO;
import com.ecoolex.chinadutyfree.retailer.ws.DirectMailGoodsService;
import com.ecoolex.chinadutyfree.retailer.ws.GoodsService;
import com.ecoolex.framework.common.annotation.Description;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;

@GraphQL2Module(type = Type.User, description = "商品分类查询")
public class GoodsCategoryGraphql {

	@Reference
	private GoodsService goodsService;

	@Reference
	private DirectMailGoodsService directMailGoodsService;

	@Autowired
	private StorageService storageService;

	@TypeField(type = GoodsCategory.class, field = "parent", description = "父分类")
	public GoodsCategory parent(@Source GoodsCategory source) {
		if (Check.notNullAndGtZero(source.getParentCategoryId())) {
			return goodsService.getCategoryById(source.getParentCategoryId());
		}
		return null;
	}

	@TypeField(type = GoodsCategory.class, field = "child", description = "子分类列表")
	public List<GoodsCategory> child(@Source GoodsCategory source, Boolean isShow) {
		return goodsService.queryCategoryByParentId(source.getCategoryId(), isShow);
	}

	@TypeField(type = GoodsCategory.class, field = "icon", description = "Icon")
	public StorageInfo icon(@Source GoodsCategory source) {
		return storageService.get(source.getIcon());
	}

	@TypeField(type = GoodsCategory.class, field = "image", description = "图片")
	public StorageInfo image(@Source GoodsCategory source) {
		return storageService.get(source.getImage());
	}

	@TypeField(type = GoodsCategory.class, field = "banner", description = "广告位图片")
	public StorageInfo banner(@Source GoodsCategory source) {
		return storageService.get(source.getBanner());
	}

	@TypeField(type = GoodsCategory.class, field = "directMailGoodsList", description = "直邮商品列表")
	public Pagination<DirectMailGoods> directMailGoodsList(@Source GoodsCategory source, @NonNull Sorting[] sorts, @NonNull Page page) {
		return directMailGoodsService.queryDirectMailGoodsByCategoryIdForH5(source.getCategoryId(), sorts, page);
	}

	@TypeField(type = GoodsCategoryTopChildDO.class, field = "category", description = "分类对象")
	public GoodsCategory category(@Source GoodsCategoryTopChildDO source) {
		return goodsService.getCategoryById(source.getCategoryId());
	}

	@QueryField(field = "goodsCategory", description = "查询商品分类")
	public GoodsCategory get(@NonNull Integer categoryId) {
		return goodsService.getCategoryById(categoryId);
	}

	@QueryField(field = "goodsCategoryList", description = "用户查询商品分类列表")
	public Pagination<GoodsCategory> queryCategoryList(@Description({ "商品分类等级" }) @NonNull Integer level, @Description({
		"当前分类的父类id" }) Integer parentCategoryId, @NonNull Sorting[] sorts, @NonNull Page page) {
		return goodsService.queryGoodsCategoryForH5(level, parentCategoryId, sorts, page);
	}

	@QueryField(field = "goodsCategoryListTopChild", description = "查询商品分类列表的顶级子类列表")
	public List<GoodsCategoryTopChildDO> queryGoodsCategoryTopChild(@Description({
		"最少直邮商品数量" }) @NonNull Integer minDirectMailGoodsNumber, @NonNull Integer size) {
		return goodsService.queryGoodsCategoryTopChild(minDirectMailGoodsNumber, size);
	}
}
